package com.wubai.subject.domain.handler.subject;

import com.wubai.subject.common.enums.IsDeletedFlagEnum;
import com.wubai.subject.common.enums.SubjectInfoTypeEnum;

import com.wubai.subject.domain.convert.SubjectMutipleConverter;
import com.wubai.subject.domain.entity.SubjectAnswerBO;
import com.wubai.subject.domain.entity.SubjectInfoBO;
import com.wubai.subject.domain.entity.SubjectOptionBO;

import com.wubai.subject.infra.basic.entity.SubjectMultiple;

import com.wubai.subject.infra.basic.service.SubjectMultipleService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Component
public class MutipleTyperHandler implements SubjectTypeHandler{


    @Resource
    private SubjectMultipleService subjectMultipleService;



    @Override
    public SubjectInfoTypeEnum getHandlerType() {
        return SubjectInfoTypeEnum.MULTIPLE;
    }

    @Override
    public void add(SubjectInfoBO subjectInfoBO) {

       // List<SubjectBrief> subjectBriefList = SubjectBriefConverter.INSTANCE.convertBOToBriefList(subjectInfoBO.getOptionList());
        List<SubjectMultiple> subjectMultipleList = SubjectMutipleConverter.INSTANCE.convertBOToMutipleList(subjectInfoBO.getOptionList());
        subjectMultipleList.forEach(subjectMultiple->{

            subjectMultiple.setSubjectId(subjectInfoBO.getId());
            subjectMultiple.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());

        });
        subjectMultipleService.batchInsert(subjectMultipleList);



    }

    @Override
    public SubjectOptionBO query(int subjectId) {
        SubjectMultiple subjectMultiple = new SubjectMultiple();
        subjectMultiple.setSubjectId(Long.valueOf(subjectId));
        List<SubjectMultiple> result = subjectMultipleService.queryByCondition(subjectMultiple);
        List<SubjectAnswerBO> subjectAnswerBOList = SubjectMutipleConverter.INSTANCE.convertEntityToBoList(result);
        SubjectOptionBO subjectOptionBO = new SubjectOptionBO();
        subjectOptionBO.setOptionList(subjectAnswerBOList);
        return subjectOptionBO;
    }
}
